package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.gift.NoviceItemData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.GlideCircleTransform;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.widget.screenadaptaion.CustomLinearlayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/19.
 */

public class GiftAdapter extends BaseAdapter {

    private Context context;
    private List<NoviceItemData> list = new ArrayList<>();
    private JSONObject goods;
    private JSONObject item;
    private JSONObject market;
    private JSONObject group;
    private JSONObject avatar;


    public GiftAdapter(Context context, List<NoviceItemData> data) {
        this.context = context;
        this.list = data;
    }

    public JSONObject getGoods() {
        return goods;
    }

    public void setGoods(JSONObject goods) {
        this.goods = goods;
    }

    public JSONObject getItem() {
        return item;
    }

    public void setItem(JSONObject item) {
        this.item = item;
    }

    public JSONObject getMarket() {
        return market;
    }

    public void setMarket(JSONObject market) {
        this.market = market;
    }

    public void setGroup(JSONObject group) {
        this.group = group;
    }

    public void setAvatar(JSONObject avatar) {
        this.avatar = avatar;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_user_gift, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            NoviceItemData data = list.get(position);
            holder.mGiftActivityName.setText(data.getName());
            Glide.with(UiUtils.getContext()).load(Url.IMG_URL + data.getImages()).
                    placeholder(R.mipmap.iv_loading).into(holder.mGiftProjectIcon);
            switch (data.getAtype()) {
                case 1:
                    JSONObject mItemObject = item.optJSONObject(data.getRefid());
                    holder.mGiftProjectName.setText(mItemObject.optString("name"));
                    holder.mGiftProjectMarketPrice.setText("原价: " + mItemObject.optString("marketprice"));
                    holder.mGiftProjectMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    break;
                case 2:  // 超市商品
                    JSONObject mMarketObject = market.optJSONObject(data.getRefid());
                    holder.mGiftProjectName.setText(mMarketObject.optString("name"));
                    holder.mGiftProjectMarketPrice.setText("原价: " + mMarketObject.optString("marketprice"));
                    holder.mGiftProjectMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    break;
                case 3:  // 自定义商品
                    JSONObject mGoodsObject = goods.optJSONObject(data.getRefid());
                    holder.mGiftProjectName.setText(mGoodsObject.optString("name"));
                    holder.mGiftProjectMarketPrice.setText("原价: " + mGoodsObject.optString("marketprice"));
                    holder.mGiftProjectMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    break;
            }
            if (!TextUtils.isEmpty(data.getCpfparticipants())) {
                holder.mTvPartner.setVisibility(View.VISIBLE);
            } else {
                holder.mTvPartner.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(data.getRedbeanfparticipants())) {
                holder.mTvHd.setVisibility(View.VISIBLE);
            } else {
                holder.mTvHd.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(data.getInnerfparticipants())) {
                holder.mTvMd.setVisibility(View.VISIBLE);
            } else {
                holder.mTvMd.setVisibility(View.GONE);
            }

            if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) == 0) {
                holder.mTvRemainDay.setText("即将上线");
                holder.mTvRemainDay.setBackgroundColor(UiUtils.getColor(R.color.postCartItem));
                holder.mGiftProjectGroupPrice.setText("开团价: " + data.getAprice() + " 元");
            } else if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) == -1) {
                holder.mTvRemainDay.setText("商品卖完");
                holder.mTvRemainDay.setBackgroundColor(UiUtils.getColor(R.color.postCartItem));
                holder.mGiftProjectGroupPrice.setText("团购价: " + data.getAprice() + " 元");
            } else if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) > 10) {
                holder.mTvRemainDay.setText("剩余 " + CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) + "天");
                holder.mTvRemainDay.setBackgroundColor(UiUtils.getColor(R.color.black));
                holder.mGiftProjectGroupPrice.setText("团购价: " + data.getAprice() + " 元");
            } else if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) == 1) {
                holder.mTvRemainDay.setText("剩余 01天");
                holder.mTvRemainDay.setBackgroundColor(UiUtils.getColor(R.color.title_red));
                holder.mGiftProjectGroupPrice.setText("团购价: " + data.getAprice() + " 元");
            } else {
                holder.mTvRemainDay.setText("剩余 0" + CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) + "天");
                holder.mTvRemainDay.setBackgroundColor(UiUtils.getColor(R.color.black));
                holder.mGiftProjectGroupPrice.setText("团购价: " + data.getAprice() + " 元");
            }

            if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) == 0) {
                holder.mTvGroup.setText("组成三人团奖团长: ");
                holder.mTvRewardHd.setVisibility(View.VISIBLE);
                holder.mTvRewardHd.setText(data.getHighestrewardback() + " 元");
                holder.mLlContainerImage.setVisibility(View.GONE);
                holder.mTvStartgroup.setText("去邀约");
            } else {
                if (group != null && !"".equals(group)) {
                    holder.mTvGroup.setText("团: ");
                    holder.mTvRewardHd.setVisibility(View.GONE);
                    holder.mLlContainerImage.setVisibility(View.VISIBLE);
                    String mS = group.optString(data.getId());
                    if (avatar != null && !"".equals(avatar)) {
                        JSONArray mJSONArray = avatar.optJSONArray(mS);
                        if (mJSONArray != null && mJSONArray.length() > 0) {
                            if (mJSONArray.length() == 1) {
                                String mO = (String) mJSONArray.get(0);
                                setIcon(mO, holder.mIvImage1);
                                holder.mTvStartgroup.setText("去邀约");

                            } else if (mJSONArray.length() == 2) {
                                String mO1 = (String) mJSONArray.get(0);
                                String mO2 = (String) mJSONArray.get(1);
                                setIcon(mO1, holder.mIvImage1);
                                setIcon(mO2, holder.mIvImage2);
                                holder.mTvStartgroup.setText("去邀约");

                            } else if (mJSONArray.length() == 3) {

                                String mO1 = (String) mJSONArray.get(0);
                                String mO2 = (String) mJSONArray.get(1);
                                String mO3 = (String) mJSONArray.get(2);
                                setIcon(mO1, holder.mIvImage1);
                                setIcon(mO2, holder.mIvImage2);
                                setIcon(mO3, holder.mIvImage3);
                                holder.mTvStartgroup.setText("看详情");

                            }
                        } else {
                            holder.mTvGroup.setText("组成三人团奖团长: ");
                            holder.mTvRewardHd.setVisibility(View.VISIBLE);
                            holder.mTvRewardHd.setText(data.getHighestrewardback() + " 元");
                            holder.mLlContainerImage.setVisibility(View.GONE);
                        }
                    }
                } else {
                    holder.mTvGroup.setText("组成三人团奖团长: ");
                    holder.mTvRewardHd.setVisibility(View.VISIBLE);
                    holder.mTvRewardHd.setText(data.getHighestrewardback() + " 元");
                    holder.mLlContainerImage.setVisibility(View.GONE);
                }
                if (CommonTool.TimeCompareTime(data.getStime(), data.getEtime()) == -1) {
                    holder.mTvStartgroup.setText("已完毕");
                }
//                else {
//                    holder.mTvStartgroup.setText("去开团");
//                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;

    }

    private void setIcon(String value, ImageView imageView) {
        if (value.startsWith("http://") || value.startsWith("https://")) {
            Glide.with(UiUtils.getContext())
                    .load(value)
                    .placeholder(R.mipmap.iv_avatar).transform(new GlideCircleTransform(UiUtils.getContext()))
                    .into(imageView);
        } else {
            Glide.with(UiUtils.getContext())
                    .load(Url.BASE_URL + value)
                    .placeholder(R.mipmap.iv_avatar).transform(new GlideCircleTransform(UiUtils.getContext()))
                    .into(imageView);
        }
    }

    class ViewHolder {
        @Bind(R.id.tv_remain_day)
        TextView mTvRemainDay;
        @Bind(R.id.gift_activity_name)
        TextView mGiftActivityName;
        @Bind(R.id.gift_project_icon)
        ImageView mGiftProjectIcon;
        @Bind(R.id.gift_project_name)
        TextView mGiftProjectName;
        @Bind(R.id.gift_project_groupPrice)
        TextView mGiftProjectGroupPrice;
        @Bind(R.id.gift_project_marketPrice)
        TextView mGiftProjectMarketPrice;
        @Bind(R.id.tv_group)
        TextView mTvGroup;
        @Bind(R.id.tv_rewardHd)
        TextView mTvRewardHd;
        @Bind(R.id.ll_container_image)
        CustomLinearlayout mLlContainerImage;
        @Bind(R.id.iv_go)
        ImageView mIvGo;
        @Bind(R.id.tv_startgroup)
        TextView mTvStartgroup;
        @Bind(R.id.tv_partner)
        TextView mTvPartner;
        @Bind(R.id.tv_hd)
        TextView mTvHd;
        @Bind(R.id.tv_md)
        TextView mTvMd;
        @Bind(R.id.iv_image1)
        ImageView mIvImage1;
        @Bind(R.id.iv_image2)
        ImageView mIvImage2;
        @Bind(R.id.iv_image3)
        ImageView mIvImage3;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
